package com.zhentao.utils;

public class DetectionUtil {
    // 计算标准化后的编辑距离作为相似度
    public static double calculateEditDistanceNormalized(String str1, String str2) {
        int editDistance = calculateEditDistance(str1, str2);
        int maxLength = Math.max(str1.length(), str2.length());
        return 1.0 - (double) editDistance / maxLength; // 标准化编辑距离
    }

    // 计算编辑距离
    public static int calculateEditDistance(String str1, String str2) {
        int[][] dp = new int[str1.length() + 1][str2.length() + 1];
        // 初始化第一行和第一列
        for (int i = 0; i <= str1.length(); i++) {
            dp[i][0] = i;
        }
        for (int j = 0; j <= str2.length(); j++) {
            dp[0][j] = j;
        }
        // 填充dp数组
        for (int i = 1; i <= str1.length(); i++) {
            for (int j = 1; j <= str2.length(); j++) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i][j - 1], dp[i - 1][j]));
                }
            }
        }
        // 返回右下角的值，即str1和str2的编辑距离
        return dp[str1.length()][str2.length()];
    }

    // 根据相似度评分
    public static double calculateScore(double similarity) {
        // 根据具体的相似度阈值来映射到分数范围
        if (similarity >= 0.7) {
            return 5;
        } else if (similarity >= 0.5) {
            return 4;
        } else if (similarity >= 0.3) {
            return 3;
        } else if (similarity >= 0.2) {
            return 2;
        }else if (similarity >= 0.1){
            return 1;
        }else if (similarity >= 0.05){
            return 0.4;
        } else {
            return 0;
        }
    }
}